package eu.codlab.game.gameba.nonem.input;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import eu.codlab.game.gameba.Emulator;
import eu.codlab.src.game.R;

public class VirtualKeypad extends RelativeLayout
implements View.OnTouchListener, AbstractButtonListener {

	private static final int DEAD_ZONE = 20;
	private View dpadView;
	private GameKeyListener gameKeyListener;
	private int keyStates;

	public VirtualKeypad(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void setGameKeyListener(GameKeyListener listener) {
		gameKeyListener = listener;
		
	}

	public int getKeyStates() {
		return keyStates;
	}

	public void reset() {
		keyStates = 0;
	}

	@Override
	protected void onFinishInflate() {
		//dpadView = findViewById(R.id.dpad);
		if(dpadView != null)
			dpadView.setOnTouchListener(this);

		//setTouchListener(R.id.select);
		//setTouchListener(R.id.button_l);
		//setTouchListener(R.id.button_r);
		//setTouchListener(R.id.button_a);
		//setTouchListener(R.id.button_b);
		//setTouchListener(R.id.start);
	}

	private void setTouchListener(int id){
		if(findViewById(id) != null)
			findViewById(id).setOnTouchListener(this);
	}

	private void setKeyStates(int newStates) {
		if (keyStates != newStates) {
			keyStates = newStates;
			gameKeyListener.onGameKeyChanged();
		}
	}

	private int getGameKey(float fx, float fy) {
		final int x = (int) (fx + 0.5f);
		final int y = (int) (fy + 0.5f);
		final int cx = dpadView != null ? dpadView.getWidth() / 2 : 1;
		final int cy = dpadView != null ? dpadView.getHeight() / 2 : 1;

		int key = 0;
		if (x < cx - DEAD_ZONE)
			key |= Emulator.GAMEPAD_LEFT;
		else if (x > cx + DEAD_ZONE)
			key |= Emulator.GAMEPAD_RIGHT;
		if (y < cy - DEAD_ZONE)
			key |= Emulator.GAMEPAD_UP;
		else if (y > cy + DEAD_ZONE)
			key |= Emulator.GAMEPAD_DOWN;

		return key;
	}

	private boolean onDpadTouch(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
		case MotionEvent.ACTION_MOVE:
			setKeyStates(getGameKey(event.getX(), event.getY()));
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_OUTSIDE:
		case MotionEvent.ACTION_CANCEL:
			setKeyStates(0);
			break;
		default:
			return false;
		}
		return true;
	}

	private boolean onButtonTouch(int id, MotionEvent event) {
		/*int gameKey;
		switch (id) {
		case R.id.button_a:
			gameKey = Emulator.GAMEPAD_A;
			break;
		case R.id.button_b:
			gameKey = Emulator.GAMEPAD_B;
			break;
		case R.id.button_r:
			gameKey = Emulator.GAMEPAD_TR;
			break;
		case R.id.button_l:
			gameKey = Emulator.GAMEPAD_TL;
			break;
		case R.id.select:
			gameKey = Emulator.GAMEPAD_SELECT;
			break;
		case R.id.start:
			gameKey = Emulator.GAMEPAD_START;
			break;
		default:
			return false;
		}

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			keyStates |= gameKey;
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_OUTSIDE:
		case MotionEvent.ACTION_CANCEL:
			keyStates &= ~gameKey;
			break;
		default:
			return false;
		}
		gameKeyListener.onGameKeyChanged();*/
		return true;
	}

	public boolean onTouch(View v, MotionEvent event) {
		int id = v.getId();
		//if (id == R.id.dpad)
		//	return onDpadTouch(event);
		return onButtonTouch(id, event);
	}

	@Override
	public void onKeyFromClient(AbstractButtonServer server, int keycode,
			int state) {
		if(state == 1){
			switch(keycode){
			case Emulator.GAMEPAD_A:
				keyStates |= Emulator.GAMEPAD_A;
				break;
			case Emulator.GAMEPAD_B:
				keyStates |= Emulator.GAMEPAD_B;
				break;
			case Emulator.GAMEPAD_TR:
				keyStates |= Emulator.GAMEPAD_TR;
				break;
			case Emulator.GAMEPAD_TL:
				keyStates |= Emulator.GAMEPAD_TL;
				break;
			case Emulator.GAMEPAD_SELECT:
				keyStates |= Emulator.GAMEPAD_SELECT;
				break;
			case Emulator.GAMEPAD_START:
				keyStates |= Emulator.GAMEPAD_START;
				break;
			default:
				return;
			}
		}else if(state == 0){
			switch(keycode){
			case Emulator.GAMEPAD_A:
				keyStates &= ~Emulator.GAMEPAD_A;
				break;
			case Emulator.GAMEPAD_B:
				keyStates &= ~Emulator.GAMEPAD_B;
				break;
			case Emulator.GAMEPAD_TR:
				keyStates &= ~Emulator.GAMEPAD_TR;
				break;
			case Emulator.GAMEPAD_TL:
				keyStates &= ~Emulator.GAMEPAD_TL;
				break;
			case Emulator.GAMEPAD_SELECT:
				keyStates &= ~Emulator.GAMEPAD_SELECT;
				break;
			case Emulator.GAMEPAD_START:
				keyStates &= ~Emulator.GAMEPAD_START;
				break;
			default:
				return;
			}
		}else if(state == 2){
			keyStates = keycode;
		}
		gameKeyListener.onGameKeyChanged();
		return;
	}
}
